import 'package:flutter/cupertino.dart';
import '../public.dart';

/*
 * 生日选择
 */
class BirthdayPicker extends StatefulWidget {
  final Function onPressed;
  final CupertinoDatePickerMode mode;
  final DateTime initialDateTime;
  BirthdayPicker(
      {Key key,
      this.onPressed,
      this.mode = CupertinoDatePickerMode.date,
      this.initialDateTime})
      : super(key: key);
  @override
  _BirthdayPickerState createState() => _BirthdayPickerState();
}

class _BirthdayPickerState extends State<BirthdayPicker> {
  DateTime _initialDateTime;
  DateTime _dateTime;
  DateTime _nowDateTime = DateTime.now();
  String birthdayString;
  String lunarBirthdayString;
  TextStyle style;
  @override
  void initState() {
    super.initState();
    setState(() {
      _initialDateTime = widget.initialDateTime ?? DateTime.now();
      birthdayString = UDateUtil.getBirthdayString(_initialDateTime);
      lunarBirthdayString = UDateUtil.getLunarBirthdayString(_initialDateTime);
      _dateTime = _initialDateTime;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(children: [
      Container(
        height: 56,
        color: ColorUtil.rgba(255, 255, 255, 0.05),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            FlatButton(
                height: double.infinity,
                onPressed: () {
                  Navigator.pop(context);
                },
                child: Text(
                  '取消',
                  style: TextStyle(
                    fontSize: 18,
                    color: Colors.white30,
                  ),
                )),
            FlatButton(
                height: double.infinity,
                onPressed: () {
                  widget.onPressed(_dateTime);
                  Navigator.pop(context);
                },
                child: Text(
                  '确定',
                  style: TextStyle(
                    fontSize: 18,
                    color: Colors.white,
                  ),
                ))
          ],
        ),
      ),
      Container(
          height: 48,
          margin: EdgeInsets.only(left: 10, bottom: 10, right: 10),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                '$birthdayString',
                style: TextStyle(color: Colors.white, fontSize: 14),
              ),
              Text(
                '$lunarBirthdayString',
                style: TextStyle(color: Colors.white, fontSize: 14),
              )
            ],
          )),
      SizedBox(
          height: 152,
          child: CupertinoTheme(
              data: CupertinoThemeData(
                barBackgroundColor: Colors.red,
                primaryContrastingColor: Colors.black,
                textTheme: CupertinoTextThemeData(
                  dateTimePickerTextStyle:
                      TextStyle(fontSize: 18, color: Colors.white),
                ),
              ),
              child: CupertinoDatePicker(
                initialDateTime: _initialDateTime,
                mode: widget.mode,
                onDateTimeChanged: (date) {
                  setState(() {
                    birthdayString = UDateUtil.getBirthdayString(date);
                    lunarBirthdayString =
                        UDateUtil.getLunarBirthdayString(date);
                    _dateTime = date;
                  });
                },
                minimumYear: _nowDateTime.year - 200 > 1900
                    ? _nowDateTime.year - 200
                    : 1900,
                maximumYear: _nowDateTime.year,
              )))
    ]);
  }
}
